package com.ws.webpacket.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.annotation.Nullable;

public class LoadingView extends View {


    private Animation animation;
    private boolean isLoding = false;

    public LoadingView(Context context) {
        super(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void loding(){
        setVisibility(VISIBLE);
        isLoding = true;
        if(animation == null){
            animation = new RotateAnimation(0,360,
                    Animation.RELATIVE_TO_SELF,0.5f,
                    Animation.RELATIVE_TO_SELF,0.5f);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setDuration(1500);
            animation.setAnimationListener(animationListener);
            animation.setFillAfter(true);
        }
        startAnimation(animation);
    }

    public void stopLode(){
        isLoding = false;
        setVisibility(GONE);
        clearAnimation();
    }

    private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if(isLoding){
                clearAnimation();
                startAnimation(animation);
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };


}
